package com.asd.system.service.Impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.asd.common.Result;
import com.asd.system.entity.Organization;
import com.asd.system.entity.Role;
import com.asd.system.entity.User;
import com.asd.system.mapper.OrganizationMapper;
import com.asd.system.mapper.RoleMapper;
import com.asd.system.mapper.UserMapper;
import com.asd.system.service.OrganizationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class OrganizationServiceImpl extends ServiceImpl<OrganizationMapper, Organization> implements OrganizationService {

    @Autowired
    private OrganizationMapper organizationMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;


    @Override
    public Result<?> removeOrgMembers(String id, String userId) {

        Result<Organization> result = new Result<Organization>();

        Organization organization = organizationMapper.selectById(id);

        String admin = organization.getAdmin();

        User user = userMapper.selectById(userId);

        List<String> list = (List<String>) JSONArray.parse(admin);
        //判断list中是否包含userName
        if (list.contains(user.getUserName())) {
            result.error500("请修改用户角色为“用户“后再次操作！");
        } else {

            user.setRoleId("");
            //删除组织将用户中的组织id置空
            user.setOrgId("");
            //冻结用户
            user.setIsEnable("0");

            userMapper.updateById(user);

            result.success("删除成功");
        }

        return result;
    }

    @Override
    public boolean updateOrgBasicInfo(String id, String name, String note) {

        organizationMapper.updateOrgBasicInfo(id, name, note);

        return true;
    }

    @Override
    public Result<?> removeOrgAdmin(String id, String userName) {

        Result<Organization> result = new Result<>();

        Organization organization = organizationMapper.selectById(id);

        User user = userMapper.findToName(userName);

        List<String> list = (List<String>) JSONArray.parse(organization.getAdmin());

        if (list.contains(userName)) {

            if (list.size()==1){
                result.error500("组织管理员不能为空！");
            }else {
                list.remove(userName);

                String str = JSON.toJSONString(list);

                organizationMapper.updateAdminById(id, str);
                user.setRoleId("1454377155839512578");
//            //删除组织将用户中的组织id置空
//            user.setOrgId("");
//            //冻结用户
//            user.setIsEnable("1");

                userMapper.updateById(user);
                result.success("管理员删除成功");
            }
        } else {
            result.error500("非本组织管理员不能删除");
        }

        return result;
    }

    @Override
    public void removeOrgById(String id) {
        List<User> userList = userMapper.getByOrgId(id);
        for (int i = 0; i < userList.size(); i++) {
            User user = userList.get(i);
            user.setRoleId("");
            user.setIsEnable("0");
            user.setOrgId("");
            userMapper.updateById(user);
        }
        organizationMapper.deleteById(id);

    }

    @Override
    public Result<Organization> updateManager(String orgId, String userName, String roleId) {

        Result<Organization> result = new Result<Organization>();
        Organization organization = organizationMapper.selectById(orgId);
        String admin = organization.getAdmin();

        User user = userMapper.findToName(userName);

        if (user == null) {
            result.error500("用户不存在,请重新输入！");
        }else {
            Role role = roleMapper.selectById(user.getRoleId());
            if (role == null) {

                user.setOrgId(orgId);
                user.setRoleId(roleId);
                user.setIsEnable("1");
                //管理员不存在时
                if (admin == null || admin.equals("")) {
                    List<String> list = new ArrayList<String>();
                    list.add(userName);
                    String str = JSON.toJSONString(list);
                    organization.setAdmin(str);
                    //管理员存在时
                } else {
                    List<String> list = (List<String>) JSONArray.parse(admin);
                    list.add(userName);

                    String str = JSON.toJSONString(list);
                    organization.setAdmin(str);
                }
                userMapper.updateById(user);
                organizationMapper.updateById(organization);
                result.success("设置成功");

            } else {
                if (role.getName().equals("管理员")) {
                    result.error500("该用户是管理员，不能再次设置");
                } else {
                    user.setOrgId(orgId);
                    user.setRoleId(roleId);
                    //管理员不存在时
                    if (admin == null || admin.equals("")) {
                        List<String> list = new ArrayList<String>();
                        list.add(userName);
                        String str = JSON.toJSONString(list);
                        organization.setAdmin(str);
                        //管理员存在时
                    } else {
                        List<String> list = (List<String>) JSONArray.parse(admin);
                        list.add(userName);

                        String str = JSON.toJSONString(list);
                        organization.setAdmin(str);
                    }
                    userMapper.updateById(user);
                    organizationMapper.updateById(organization);
                    result.success("设置成功");
                }
            }
        }

        return result;
    }

    @Override
    public Result<Organization> addOrgMembers(String orgId, String userName, String roleId) {

        Result<Organization> result = new Result<Organization>();

        //激活用户
        User user = userMapper.findToName(userName);
        Role role = roleMapper.selectById(roleId);
        Organization organization = organizationMapper.selectById(orgId);

        if (user == null) {
            result.error500("未找到对应用户");

        } else {
            Role role1 = roleMapper.selectById(user.getRoleId());
            if (role1 == null) {//该用户不存在角色时

                //判断是否是添加管理员
                if (role.getName().equals("管理员")) {

                    user.setIsEnable("1");
                    user.setRoleId(roleId);
                    user.setOrgId(orgId);

                    String admin = organization.getAdmin();
                    //管理员不存在时
                    if (admin == null || admin.equals("")) {
                        List<String> list = new ArrayList<String>();
                        list.add(userName);
                        String str = JSON.toJSONString(list);
                        organization.setAdmin(str);
                    } else {//管理员存在时

                        List<String> list = (List<String>) JSONArray.parse(admin);
                        list.add(userName);
                        String str = JSON.toJSONString(list);
                        organization.setAdmin(str);

                    }
                    userMapper.updateById(user);
                    organizationMapper.updateById(organization);
                    result.success("添加成功");
                    //添加普通成员
                } else {

                    user.setIsEnable("1");
                    user.setRoleId(roleId);
                    user.setOrgId(orgId);

                    userMapper.updateById(user);
                    result.success("添加成功");
                }

                //该用户存在角色时
            } else {

                result.error500("该账号已存在组织，请退出其他组织后重新添加");
//
//                if (role1.getName().equals("管理员")) {
//                    result.error500("该用户是管理员，不能再次添加！");
//                } else {
//                    if (user.getOrgId().equals(orgId)) {
//                        result.error500("用户已在本组织，不能重复添加");
//                    } else {
//                        //判断是否是添加管理员
//                        if (role.getName().equals("管理员")) {
//
//                            user.setRoleId(roleId);
//                            user.setOrgId(orgId);
//
//                            String admin = organization.getAdmin();
//                            //管理员不存在时
//                            if (admin == null || admin.equals("")) {
//                                List<String> list = new ArrayList<String>();
//                                list.add(userName);
//                                String str = JSON.toJSONString(list);
//                                organization.setAdmin(str);
//                            } else {//管理员存在时
//
//                                List<String> list = (List<String>) JSONArray.parse(admin);
//                                list.add(userName);
//                                String str = JSON.toJSONString(list);
//                                organization.setAdmin(str);
//
//                            }
//
//                            userMapper.updateById(user);
//                            organizationMapper.updateById(organization);
//                            result.success("添加成功");
//                            //添加普通成员
//                        } else {
//
//                            user.setRoleId(roleId);
//                            user.setOrgId(orgId);
//
//                            userMapper.updateById(user);
//                            result.success("添加成功");
//                        }
//                        user.setRoleId(roleId);
//                        user.setOrgId(orgId);
//
//                        userMapper.updateById(user);
//                        result.success("添加成功");
//                    }
//                }
            }
        }

        return result;
    }

//    @Override
//    public void updateAdminById(String orgId, String str) {
//        organizationMapper.updateAdminById(orgId,str);
//    }
}
